package com.learn.xueer.ui.adapter

import android.widget.ImageView
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.learn.xueer.R
import com.learn.xueer.app.util.RoundedCornersTransform
import com.learn.xueer.data.model.bean.BookInfo
import com.learn.xueer.data.model.bean.HearSayResponse

class HearBookAdapter(data: MutableList<HearSayResponse>) :
    BaseQuickAdapter<HearSayResponse, BaseViewHolder>(
        R.layout.item_hear_book, data
    ) {
    override fun convert(holder: BaseViewHolder, item: HearSayResponse) {
        holder.setText(R.id.name, item.productName)
        val bookInfos = Gson().fromJson<MutableList<BookInfo>>(
            item.packageinfo,
            object : TypeToken<MutableList<BookInfo>>() {}.type
        )
        var countStr = ""
        bookInfos.forEach {
            countStr = countStr + it.title + it.content + "\r\n"
        }
        countStr.removeRange(countStr.length - 1, countStr.length)
        holder.setText(R.id.count, countStr)

        val roundedCornersTransform = RoundedCornersTransform(context,SizeUtils.dp2px(15F).toFloat())
        roundedCornersTransform.setNeedCorner(
            leftTop = true,
            rightTop = true,
            leftBottom = false,
            rightBottom = false
        )

        val bitmapTransform =
            RequestOptions.bitmapTransform(roundedCornersTransform)

        Glide.with(context)
            .load(item.cover_full)
            .apply(bitmapTransform)
            .placeholder(R.mipmap.default_book)
            .error(R.mipmap.default_book)
            .into(holder.getView<ImageView>(R.id.bookImage))
    }
}